package com.example.demo2.mapper;

import com.example.demo2.entity.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import org.apache.ibatis.annotations.*;
import com.example.demo2.entity.Solution;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.annotations.DeleteProvider;

import java.util.List;
import java.util.Map;
@Mapper
public interface SolutionMapper {
    @Select("select * from solution where soId=#{soId}")
    public Solution findSolutionBySoId(@Param("soId") Integer soId);
    @Select("select * from solution where eId = #{eid}")
    public List<Solution> getAllSolutionsOfEquById(@Param("eid") int eid);

    @Update("update solution set factory= #{solution.factory}, picture= #{solution.picture}, soName= #{solution.soName}, soUrl= #{solution.soUrl} where soId = #{solution.soId}")
    public int modifySolution(@Param("solution") Solution solution);

    @Insert("insert into solution (eId,factory,picture,soName,soUrl) values (#{solution.eId},#{solution.factory},#{solution.picture},#{solution.soName},#{solution.soUrl})")
    public int addSolution(@Param("solution") Solution solution);

    @Transactional
    @DeleteProvider(type = SqlProvider.class, method = "deleteSoluListOfEqu")
    int deleteSoluListOfEqu(@Param("soids") List<Integer> soids);

    class SqlProvider {

        public String deleteSoluListOfEqu(Map<String, Object> params) {
            List<Integer> ids = (List<Integer>) params.get("soids");
            StringBuilder sql = new StringBuilder();
            sql.append("DELETE FROM solution WHERE soId IN (");
            for (int i = 0; i < ids.size(); i++) {
                sql.append(ids.get(i));
                if (i < ids.size() - 1) {
                    sql.append(",");
                }
            }
            sql.append(")");
            return sql.toString();
        }
    }
}
